#include <iostream>
#include <unistd.h>
#include <fcntl.h> // for fcntl
#include <sys/timerfd.h> // for timerfd_create
#include <stdint.h> // for uint64_t


int main(){
    // int timerfd_create(int clockid, int flags);
    int timerfd = timerfd_create(CLOCK_MONOTONIC, 0); // NONBLOCK
    if(timerfd < 0){
        std::cerr << "timerfd_create error" << std::endl;
        return -1;
    }
    std::cout << "timerfd: " << timerfd << std::endl;
    
    itimerspec itime;
    itime.it_value.tv_sec = 2;
    itime.it_value.tv_nsec = 0;
    itime.it_interval.tv_sec = 2;
    itime.it_interval.tv_nsec = 0;

    timerfd_settime(timerfd, 0, &itime, nullptr);

    while(true){
        uint64_t howmany;
        int ret = read(timerfd, &howmany, 8);
        if(ret < 0){
            std::cerr << "read error" << std::endl;
            return -1;
        }
        std::cout << "read " << ret << " bytes, howmany: " << howmany << std::endl;
    }
    return 0;
}